import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 用户信息
  const userInfo = ref<any>(null)
  const token = ref<string>('')

  // 设置用户信息
  const setUserInfo = (info: any) => {
    userInfo.value = info
  }

  // 设置token
  const setToken = (newToken: string) => {
    token.value = newToken
    sessionStorage.setItem('token', newToken)
  }

  // 清除用户数据
  const clearUserData = () => {
    userInfo.value = null
    token.value = ''
    sessionStorage.removeItem('token')
  }

  // 初始化用户数据
  const initUserData = () => {
    const storedToken = sessionStorage.getItem('token')
    if (storedToken) {
      token.value = storedToken
    }
  }

  return {
    userInfo,
    token,
    setUserInfo,
    setToken,
    clearUserData,
    initUserData
  }
}) 